package com.furnitur.core.mapper.order;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.furnitur.core.entity.order.Order;
import com.furnitur.modules.admin.dto.statistics.OrderStatisticsDTO;
import com.furnitur.modules.admin.vo.order.MerchantOrderStatisticsVO.DailyOrderStats;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Mapper
public interface OrderMapper extends BaseMapper<Order> {
    
    Double getTodaySales(@Param("merchantId") Long merchantId);
    
    Double getYesterdaySales(@Param("merchantId") Long merchantId);
    
    Integer getTodayOrders(@Param("merchantId") Long merchantId);
    
    Integer getYesterdayOrders(@Param("merchantId") Long merchantId);
    
    Integer countPendingOrders(@Param("merchantId") Long merchantId);
    
    Integer countTotalOrders(@Param("dto") OrderStatisticsDTO dto);
    
    Double sumTotalSales(@Param("dto") OrderStatisticsDTO dto);
    
    Integer countRefundOrders(@Param("merchantId") Long merchantId);
    
    Integer countActiveUsers(@Param("dto") OrderStatisticsDTO dto);
    
    List<Map<String, Object>> getOrderTrends(@Param("dto") OrderStatisticsDTO dto);
    
    List<Map<String, Object>> getSalesTrends(@Param("dto") OrderStatisticsDTO dto);
    
    List<Map<String, Object>> getCategoryStats(@Param("merchantId") Long merchantId);
    
    boolean verifyOwnership(@Param("resourceId") Long resourceId, @Param("merchantId") Long merchantId);
    
    /**
     * 获取订单状态分布
     */
    List<Map<String, Object>> getStatusDistribution(@Param("dto") OrderStatisticsDTO dto);
    
    /**
     * 统计已完成订单数
     */
    Integer countCompletedOrders(@Param("dto") OrderStatisticsDTO dto);
    
    /**
     * 统计复购用户数
     */
    Integer countRepeatUsers(@Param("dto") OrderStatisticsDTO dto);

    /**
     * 查询商家订单总金额
     */
    @Select("SELECT COALESCE(SUM(total_amount), 0) FROM orders WHERE merchant_id = #{merchantId} AND status != 4")
    BigDecimal sumOrderAmount(@Param("merchantId") Long merchantId);
    
    /**
     * 获取每日订单统计
     */
    @Select("SELECT " +
            "DATE_FORMAT(created_at, '%Y-%m-%d') as date, " +
            "COUNT(*) as orderCount, " +
            "COALESCE(SUM(total_amount), 0) as orderAmount " +
            "FROM orders " +
            "WHERE merchant_id = #{merchantId} " +
            "AND status != 4 " +
            "GROUP BY DATE_FORMAT(created_at, '%Y-%m-%d') " +
            "ORDER BY date DESC " +
            "LIMIT 7")
    List<DailyOrderStats> getDailyOrderStats(@Param("merchantId") Long merchantId);

    // 根据用户ID获取商家ID
    Long getMerchantIdByUserId(Long userId);

    /**
     * 计算订单完成率
     */
    @Select("SELECT IFNULL(" +
            "  (SELECT COUNT(*) FROM orders WHERE status = 3 AND merchant_id = #{merchantId}) * 100.0 / " +
            "  NULLIF((SELECT COUNT(*) FROM orders WHERE status != 4 AND merchant_id = #{merchantId}), 0)," +
            "  0" +
            ") as completion_rate")
    Double calculateCompletionRate(@Param("merchantId") Long merchantId);

    /**
     * 计算平均客单价
     */
    @Select("SELECT IFNULL(" +
            "  AVG(total_amount)," +
            "  0" +
            ") as average_order_value " +
            "FROM orders " +
            "WHERE status = 3 AND merchant_id = #{merchantId}")
    BigDecimal calculateAverageOrderValue(@Param("merchantId") Long merchantId);

    /**
     * 根据商家ID删除订单
     */
    @Delete("DELETE FROM orders WHERE merchant_id = #{merchantId}")
    int deleteByMerchantId(@Param("merchantId") Long merchantId);
}